Conditions | 1 |
Paths | 1 |
Total Lines | 67 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | define(['polyglot', 'moment', 'helper'], function (Polyglot, moment, helper) { |
||
3 | return function () { |
||
4 | var router; |
||
5 | |||
6 | function languageSelect(el) { |
||
7 | var select = document.createElement('select'); |
||
8 | select.className = 'language-switch'; |
||
9 | select.setAttribute('aria-label', 'Language'); |
||
10 | select.addEventListener('change', setSelectLocale); |
||
11 | el.appendChild(select); |
||
12 | |||
13 | // Keep english |
||
14 | select.innerHTML = '<option>Language</option>'; |
||
15 | for (var i = 0; i < config.supportedLocale.length; i++) { |
||
16 | select.innerHTML += '<option value="' + config.supportedLocale[i] + '">' + config.supportedLocale[i] + '</option>'; |
||
17 | } |
||
18 | } |
||
19 | |||
20 | function setSelectLocale(event) { |
||
21 | router.fullUrl({ lang: event.target.value }, false, true); |
||
22 | } |
||
23 | |||
24 | function getLocale(input) { |
||
25 | var language = input || navigator.languages && navigator.languages[0] || navigator.language || navigator.userLanguage; |
||
26 | var locale = config.supportedLocale[0]; |
||
27 | config.supportedLocale.some(function (item) { |
||
28 | if (language.indexOf(item) !== -1) { |
||
29 | locale = item; |
||
30 | return true; |
||
31 | } |
||
32 | return false; |
||
33 | }); |
||
34 | return locale; |
||
35 | } |
||
36 | |||
37 | function setTranslation(json) { |
||
38 | _.extend(json); |
||
39 | |||
40 | if (moment.locale(_.locale()) !== _.locale()) { |
||
41 | moment.defineLocale(_.locale(), { |
||
42 | longDateFormat: { |
||
43 | LT: 'HH:mm', |
||
44 | LTS: 'HH:mm:ss', |
||
45 | L: 'DD.MM.YYYY', |
||
46 | LL: 'D. MMMM YYYY', |
||
47 | LLL: 'D. MMMM YYYY HH:mm', |
||
48 | LLLL: 'dddd, D. MMMM YYYY HH:mm' |
||
49 | }, |
||
50 | calendar: json.momentjs.calendar, |
||
51 | relativeTime: json.momentjs.relativeTime |
||
52 | }); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | function init(r) { |
||
57 | router = r; |
||
58 | /** global: _ */ |
||
59 | window._ = new Polyglot({ locale: getLocale(router.getLang()), allowMissing: true }); |
||
60 | helper.getJSON('locale/' + _.locale() + '.json?' + config.cacheBreaker).then(setTranslation); |
||
61 | document.querySelector('html').setAttribute('lang', _.locale()); |
||
62 | } |
||
63 | |||
64 | return { |
||
65 | init: init, |
||
66 | getLocale: getLocale, |
||
67 | languageSelect: languageSelect |
||
68 | }; |
||
69 | }; |
||
70 | }); |
||
71 |